package com.douqu.game.fileManager;

import com.bean.core.util.Utils;
import com.douqu.game.*;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.PlatformFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: miralce.
 * Description: 文件管理菜单
 * Date: 2018/8/8 0008 下午 3:34
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
public class FileManager extends MenuManager {
    //文件管理工具
    private JTextField excelInPathText, excelOutPathText, encodeOutPathText, remoteDirectoryText, uploadPathText;

    //文件管理button
    private JButton excelInPathBtn, excelOutPathBtn, encodeOutPathBtn, uploadPathBtn, changeBtn, encodeBtn, testBtn, speedBtn, showBtn, uploadBtn, clearBtn;

    public static String EXCEL_IN, EXCEL_OUT, ENCODE_OUT, UPLOAD_IN, SERVER_BAT_PATH;

    public FileManager(Container container, MainFrame mainFrame) {
        super(container, mainFrame);

        initRemark("<html>1.Excel输入 -> 点击后选择要转换的Excel文件所在的目录<br/>" +
                "2.Excel输出 -> 点击后选择Excel转换后的文件存放的目录<br/>" +
                "3.加密输出 -> 点击后选择加密后的文件存放的目录<br/>" +
                "4.上传目录 -> 点击后选择要上传到资源服务器的本地目录(不允许有中文)<br/>" +
                "<font color='white'>5.加密完成后,必须先进行测试,并且测试没有错误再上传</font><br/>" +
                "6.输入资源服务器目录,开始上传,过程中不要关闭窗口<br/>" +
                "<font color='white'>7.开始操作之前请确认几个目录路径是否正确(所有路径不能有中文)</font></html>",150);

        excelInPathBtn = new JButton("Excel输入");
        excelInPathBtn.setBounds(20,170,120,30);
        excelInPathBtn.setFont(MainFrame.font18);
        container.add(excelInPathBtn);

        excelInPathText = new JTextField(EXCEL_IN);
        excelInPathText.setBounds(150,170,470,30);
        excelInPathText.setFont(MainFrame.font18);
        excelInPathText.setEditable(false);
        container.add(excelInPathText);

        excelOutPathBtn = new JButton("Excel输出");
        excelOutPathBtn.setBounds(20,210,120,30);
        excelOutPathBtn.setFont(MainFrame.font18);
        container.add(excelOutPathBtn);

        excelOutPathText = new JTextField(EXCEL_OUT);
        excelOutPathText.setBounds(150,210,470,30);
        excelOutPathText.setFont(MainFrame.font18);
        excelOutPathText.setEditable(false);
        container.add(excelOutPathText);

        encodeOutPathBtn = new JButton("加密输出");
        encodeOutPathBtn.setBounds(20,250,120,30);
        encodeOutPathBtn.setFont(MainFrame.font18);
        container.add(encodeOutPathBtn);

        encodeOutPathText = new JTextField(ENCODE_OUT);
        encodeOutPathText.setBounds(150,250,470,30);
        encodeOutPathText.setFont(MainFrame.font18);
        encodeOutPathText.setEditable(false);
        container.add(encodeOutPathText);

        uploadPathBtn = new JButton("上传目录");
        uploadPathBtn.setBounds(20,290,120,30);
        uploadPathBtn.setFont(MainFrame.font18);
        container.add(uploadPathBtn);

        uploadPathText = new JTextField(UPLOAD_IN);
        uploadPathText.setBounds(150,290,470,30);
        uploadPathText.setFont(MainFrame.font18);
        uploadPathText.setEditable(false);
        container.add(uploadPathText);

        contentText = new JTextArea();
        contentText.setFont(new Font("宋体", 0, 16));
        contentText.setEditable(false);
        contentText.setForeground(Color.WHITE);
        contentText.setBorder(null);
        contentText.setBackground(new Color(19,71,100));

        JScrollPane infoJSP = new JScrollPane(contentText);
        infoJSP.setBounds(20, 330, 600, 320);
        container.add(infoJSP);

        changeBtn = new JButton("开始转换");
        changeBtn.setBounds(20,660,140,35);
        changeBtn.setFont(MainFrame.font22);
        container.add(changeBtn);

        encodeBtn = new JButton("开始加密");
        encodeBtn.setBounds(250,660,140,35);
        encodeBtn.setFont(MainFrame.font22);
        container.add(encodeBtn);

        testBtn = new JButton("开始测试");
        testBtn.setBounds(480,660,140,35);
        testBtn.setFont(MainFrame.font22);
        container.add(testBtn);

        speedBtn = new JButton("一键执行 转换->加密->测试");
        speedBtn.setBounds(20,710,370,35);
        speedBtn.setFont(MainFrame.font22);
        container.add(speedBtn);

        clearBtn = new JButton("清除日志");
        clearBtn.setBounds(480,710,140,35);
        clearBtn.setFont(MainFrame.font22);
        container.add(clearBtn);

        showBtn = new JButton("资源服务器");
        showBtn.setBounds(20,760,140,35);
        showBtn.setFont(new Font("微软雅黑", 0, 20));
        container.add(showBtn);

        JLabel jLabel = new JLabel("目录:");
        jLabel.setBounds(200,760,45,30);
        jLabel.setFont(MainFrame.font18);
        container.add(jLabel);

        remoteDirectoryText = new JTextField();
        remoteDirectoryText.setBounds(250,763,140,30);
        remoteDirectoryText.setFont(MainFrame.font18);
        remoteDirectoryText.setToolTipText("资源服务器目录");
        container.add(remoteDirectoryText);

        uploadBtn = new JButton("开始上传");
        uploadBtn.setBounds(480,760,140,35);
        uploadBtn.setFont(MainFrame.font22);
        uploadBtn.setEnabled(false);
        container.add(uploadBtn);

        excelInPathBtn.addActionListener((ActionEvent event)->{
            actionListener(event);
        });
        excelOutPathBtn.addActionListener((ActionEvent event)->{
            actionListener(event);
        });
        encodeOutPathBtn.addActionListener((ActionEvent event)->{
            actionListener(event);
        });
        uploadPathBtn.addActionListener((ActionEvent event)->{
            actionListener(event);
        });
        changeBtn.addActionListener((ActionEvent event)->{
            actionListener(event);
        });
        encodeBtn.addActionListener((ActionEvent event)->{
            actionListener(event);
        });
        testBtn.addActionListener((ActionEvent event)->{
            actionListener(event);
        });
        speedBtn.addActionListener((ActionEvent event)->{
            actionListener(event);
        });
        showBtn.addActionListener((ActionEvent event) -> {
            actionListener(event);
        });
        uploadBtn.addActionListener((ActionEvent event) -> {
            actionListener(event);
        });
        clearBtn.addActionListener((ActionEvent event) -> {
            actionListener(event);
        });

        initEnd("文件管理工具");
        println("资源服务器存储KEY -> " + PlatformFactory.TENCENT_COS_BUCKET);
    }

    private void actionListener(ActionEvent event){
        startActionListener();
        Object source = event.getSource();

        String result = "";
        if(source == excelInPathBtn || source == excelOutPathBtn || source == encodeOutPathBtn || source == uploadPathBtn )
        {
            JFileChooser jfc = new JFileChooser();
            jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            if(source == excelInPathBtn)
            {
                jfc.setCurrentDirectory(new File(EXCEL_IN));
                jfc.showDialog(new JLabel(), "选择Excel输入");
            }
            else if(source == excelOutPathBtn)
            {
                jfc.setCurrentDirectory(new File(EXCEL_OUT));
                jfc.showDialog(new JLabel(), "选择Excel输出");
            }
            else if(source == encodeOutPathBtn)
            {
                jfc.setCurrentDirectory(new File(ENCODE_OUT));
                jfc.showDialog(new JLabel(), "选择加密输出");
            }
            else if(source == uploadPathBtn)
            {
                jfc.setCurrentDirectory(new File(UPLOAD_IN));
                jfc.showDialog(new JLabel(), "选择上传资源");
            }

            File file = jfc.getSelectedFile();
            if(file != null)
            {
                if(source == excelInPathBtn)
                    excelInPathText.setText(file.getAbsolutePath());
                else if(source == excelOutPathBtn)
                    excelOutPathText.setText(file.getAbsolutePath());
                else if(source == encodeOutPathBtn)
                    encodeOutPathText.setText(file.getAbsolutePath());
                else if(source == uploadPathBtn)
                    uploadPathText.setText(file.getAbsolutePath());
            }
        }
        else if(source == changeBtn)
        {
            changeExcel();
        }
        else if(source == encodeBtn)
        {
            encodeConfig();
        }
        else if(source == testBtn)
        {
            if(testConfig())
            {
                uploadBtn.setEnabled(true);
            }
            else
            {
                JOptionPane.showMessageDialog(mainFrame, "配置文件有错,请检查!");
            }
        }
        else if(source == speedBtn)
        {
            if(changeExcel())
            {
                if(encodeConfig())
                {
                    if(testConfig())
                    {
                        uploadBtn.setEnabled(true);
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(mainFrame, "配置文件有错,请检查!");
                    }
                }
            }
        }
        else if(source == showBtn)
        {
            CosUtils.showDirectory();
        }
        else if(source == uploadBtn)
        {
            upload();
        }
        else if(source == clearBtn)
        {
            contentText.setText("");
        }

        endActionListener();
    }

    private boolean upload()
    {
        String remoteDirectory = remoteDirectoryText.getText();
        String localDirectory = uploadPathText.getText();

        if(Utils.isNullOrEmpty(remoteDirectory))
        {
            JOptionPane.showMessageDialog(mainFrame, "请输入要上传到的资源服务器的文件目录!");
            return false;
        }
        if(Utils.isNullOrEmpty(localDirectory))
        {
            JOptionPane.showMessageDialog(mainFrame, "请输入要上传到的本地文件目录!");
            return false;
        }

        File file = new File(localDirectory);
        if(!file.exists() || !file.isDirectory())
        {
            JOptionPane.showMessageDialog(mainFrame, localDirectory + " 文件目录不存在!");
            return false;
        }

        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(localDirectory);
        if(m.find())
        {
            JOptionPane.showMessageDialog(mainFrame, "本地上传目录不能有中文,会上传不成功!");
            return false;
        }
        if(CosUtils.uploadFile(remoteDirectory, localDirectory))
        {
            JOptionPane.showMessageDialog(mainFrame, "远程文件上传成功!");
        }
        else
        {
            JOptionPane.showMessageDialog(mainFrame, "远程文件上传失败,请检查!");
        }
        return true;
    }

    private boolean changeExcel()
    {
        String inPath = excelInPathText.getText();
        String outPath = excelOutPathText.getText();
        if(Utils.isNullOrEmpty(inPath))
        {
            JOptionPane.showMessageDialog(mainFrame, "请选择输入路径!");
            return false;
        }
        if(Utils.isNullOrEmpty(outPath))
        {
            JOptionPane.showMessageDialog(mainFrame, "请选择输出路径!");
            return false;
        }

        File file = new File(inPath);
        if(!file.exists() || !file.isDirectory())
        {
            JOptionPane.showMessageDialog(mainFrame, inPath + " 文件目录不存在!");
            return false;
        }
        File[] files = file.listFiles();
        for(File f : files)
        {
            if(f.getName().indexOf("Config") == -1 || f.getName().indexOf("Client") != -1)
                continue;

            if(f.getName().endsWith(ExcelUtils.OFFICE_EXCEL_2003_POSTFIX) || f.getName().endsWith(ExcelUtils.OFFICE_EXCEL_2010_POSTFIX))
            {
                ExcelUtils.changeExcel(f.getAbsolutePath(), f.getName().substring(0, f.getName().lastIndexOf("Config")), outPath);

            }
        }
        return true;
    }

    private boolean encodeConfig()
    {
        String inPath = excelOutPathText.getText();
        String outPath = encodeOutPathText.getText();
        if(Utils.isNullOrEmpty(inPath))
        {
            JOptionPane.showMessageDialog(mainFrame, "请选择输入路径!");
            return false;
        }
        if(Utils.isNullOrEmpty(outPath))
        {
            JOptionPane.showMessageDialog(mainFrame, "请选择输出路径!");
            return false;
        }

        File file = new File(inPath);
        if(!file.exists() || !file.isDirectory())
        {
            JOptionPane.showMessageDialog(mainFrame, inPath + " 文件目录不存在!");
            return false;
        }

        file = new File(outPath);
        if(!file.exists() || !file.isDirectory())
        {
            JOptionPane.showMessageDialog(mainFrame, outPath + " 文件目录不存在!");
            return false;
        }

        boolean encodeResult = EncodeUtils.encode(inPath, outPath);
        if(!encodeResult)
        {
            JOptionPane.showMessageDialog(mainFrame, "路径异常,请检查!");
            return false;
        }

        return true;
    }

    private boolean testConfig(String path){

        if(Utils.isNullOrEmpty(path))
        {
            JOptionPane.showMessageDialog(mainFrame, "请选择加密输出路径,即测试路径!");
            return false;
        }

        LoadTest.load(path);

        java.util.List<String> errorList = DataFactory.getInstance().getErrorList();
        if(errorList.size() > 0)
        {
            StringBuilder sb = new StringBuilder();
            for(String s : errorList)
            {
                println(s);
            }
            return false;
        }
        else
        {
            JOptionPane.showMessageDialog(mainFrame, "测试通过,可以准备上传了!");
        }
        return true;
    }

    private boolean testConfig()
    {
        String path = encodeOutPathText.getText();
        if(Utils.isNullOrEmpty(path))
        {
            JOptionPane.showMessageDialog(mainFrame, "请选择加密输出路径,即测试路径!");
            return false;
        }

        LoadTest.load(path);

        java.util.List<String> errorList = DataFactory.getInstance().getErrorList();
        if(errorList.size() > 0)
        {
            StringBuilder sb = new StringBuilder();
            for(String s : errorList)
            {
                println(s);
            }
            return false;
        }
        else
        {
            JOptionPane.showMessageDialog(mainFrame, "测试通过,可以准备上传了!");
        }
        return true;
    }

}
